package config

import (
	"errors"
	"fmt"
	"github.com/spf13/viper"
)

type Config struct {
	Mysql MysqlConfig `json:"mysql"`
	Redis RedisConfig `json:"redis"`
}

type MysqlConfig struct {
	Host      string `json:"host" yaml:"host"`
	Port      int    `json:"port" yaml:"port"`
	Username  string `json:"username"`
	Password  string `json:"password"`
	Databases string `json:"databases"`
}

type RedisConfig struct {
	Addr     string `json:"addr"`
	Password string `json:"password"`
	Db       int    `json:"db"`
}

var AppConfig Config

func InitConfig() {
	viper.SetConfigFile("../config/config.yaml")
	err := viper.ReadInConfig()
	if err != nil {
		panic(errors.New("配置路径不存在"))
		return
	}

	err = viper.Unmarshal(&AppConfig)
	if err != nil {
		panic(errors.New("配置信息解析失败"))
		return
	}

	fmt.Println(AppConfig)
}
